package com.bmf.module.rbac.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.module.rbac.entity.RbacRole;
import com.bmf.module.rbac.entity.RbacUserRole;
import com.bmf.module.rbac.mapper.RbacUserRoleMapper;
import com.bmf.module.rbac.service.RbacRoleService;
import com.bmf.module.rbac.service.RbacUserRoleService;
import com.bmf.module.rbac.vo.RbacRoleResultVo;
import com.bmf.module.rbac.vo.RbacUserRoleCreateVo;
import com.bmf.module.rbac.vo.RbacUserRoleResultVo;
import com.bmf.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * (RbacUserRole)表服务实现类
 *
 * @author makejava
 * @since 2022-12-27 15:33:18
 */
@Service("rbacUserRoleService")
public class RbacUserRoleServiceImpl extends ServiceImpl<RbacUserRoleMapper, RbacUserRole> implements RbacUserRoleService {

    @Autowired
    private RbacRoleService rbacRoleService;

    @Override
    public void delete(List<Integer> ids) {
        removeByIds(ids);
    }

    @Override
    public void insert(List<RbacUserRoleCreateVo> rbacUserRoleCreateVos) {
        List<RbacUserRole> rbacUserRoles = BeanCopyUtils.copyBeanList(rbacUserRoleCreateVos, RbacUserRole.class);
        saveBatch(rbacUserRoles);
    }

    @Override
    public List<RbacUserRoleResultVo> selectOne(Serializable userid) {
        LambdaQueryWrapper<RbacUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RbacUserRole::getUserid, userid);
        List<RbacUserRole> list = list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        List<RbacUserRoleResultVo> rbacUserRoleResultVos = BeanCopyUtils.copyBeanList(list, RbacUserRoleResultVo.class);
        List<Integer> roleids = list.stream().map(RbacUserRole::getRoleid).collect(Collectors.toList());
        List<RbacRole> rbacRoles = rbacRoleService.listByIds(roleids);
        if(CollUtil.isEmpty(rbacRoles)){
            return rbacUserRoleResultVos;
        }
        Map<Integer, RbacRole> map = rbacRoles.stream().collect(Collectors.toMap(RbacRole::getId, Function.identity()));
        rbacUserRoleResultVos.forEach(s->{
            RbacRole rbacRole = map.get(s.getRoleid());
            if(ObjectUtil.isNotEmpty(rbacRole)){
                s.setRbacRoleResultVo(BeanCopyUtils.copyBean(rbacRole, RbacRoleResultVo.class));
            }
        });
        return rbacUserRoleResultVos;
    }

    @Override
    public List<Integer> getRoleIdsByUserId(Integer id) {
        LambdaQueryWrapper<RbacUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RbacUserRole::getUserid, id).select(RbacUserRole::getRoleid);
        List<RbacUserRole> list = list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.stream().map(RbacUserRole::getRoleid).collect(Collectors.toList());
    }
}
